import { getFileListSync } from '@mintlify/prebuild';
import { join, parse, normalize } from 'path';
// TODO refactor to prebuild package
const SUPPORTED_PAGE_EXTENSIONS = ['.mdx', '.md'];
const SUPPORTED_MEDIA_EXTENSIONS = [
    '.jpeg',
    '.jpg',
    '.jfif',
    '.pjpeg',
    '.pjp',
    '.png',
    '.svg',
    '.svgz',
    '.ico',
    '.webp',
    '.gif',
    '.apng',
    '.avif',
    '.bmp',
    '.mp4',
    '.css',
];
const isNotNodeModule = (filePath) => !(filePath.dir === 'node_modules' || filePath.dir.startsWith('node_modules/'));
export const isValidPage = (filePath) => SUPPORTED_PAGE_EXTENSIONS.includes(filePath.ext);
export const isValidMedia = (filePath) => SUPPORTED_MEDIA_EXTENSIONS.includes(filePath.ext);
export const isValidLink = (filePath) => isValidPage(filePath) || isValidMedia(filePath);
export const filterNotNodeModules = (fileList) => fileList.map(normalizePath).map(parse).filter(isNotNodeModule);
export const filterPages = (fileList) => filterNotNodeModules(fileList).filter(isValidPage);
export const filterMedia = (fileList) => filterNotNodeModules(fileList).filter(isValidMedia);
export const filterLinks = (fileList) => filterNotNodeModules(fileList).filter(isValidLink);
export const getFullPath = (filePath) => join(filePath.dir, filePath.base);
export const removeFileExtension = (filePath) => join(filePath.dir, filePath.name);
export const addLeadingSlash = (filePathString) => filePathString.startsWith('/') ? filePathString : '/' + filePathString;
export const removeSelectors = (filePathString) => filePathString.split('#')[0].split('?')[0];
export const getPagePaths = (dirName) => filterPages(getFileListSync(dirName)).map(getFullPath);
export const getMediaPaths = (dirName) => filterMedia(getFileListSync(dirName)).map(getFullPath);
export const getLinkPaths = (dirName) => filterLinks(getFileListSync(dirName)).map(getFullPath);
export const normalizePath = (path) => normalize(removeLeadingSlash(path));
export const removeLeadingSlash = (filePathString) => {
    const hasLeadingSlash = filePathString.startsWith('/');
    return hasLeadingSlash ? filePathString.slice(1) : filePathString;
};
